/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

//import Enum.LoaiNguoiDungEnum;
//import java.lang.reflect.Field;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import pojo.QuyDinh;
import util.NewHibernateUtil;



/**
 *
 * @author 0
 */
public class QuyDinhDAO extends BaseDAO{   
    
   /* public static QuyDinh LayQuyDinhTheoID(String ID) //throws Exception
    {
        QuyDinh quyDinh = null;
        Session session=NewHibernateUtil.getSessionFactory().openSession();
        String hql="select qd from QuyDinh qd where qd.id = :ID";
        Query query = session.createQuery(hql);
        session.close();
        return quyDinh;
    }*/
    public static List<QuyDinh> LayDanhSachQuyDinhByID(String ID){
        List<QuyDinh> ds = null;
        Session session = NewHibernateUtil.getSessionFactory().openSession();
        String hql = "Select qd1 From QuyDinh qd1 where qd1.id = ID";
        Query query = session.createQuery(hql);
        ds = query.list();
        session.close();
        return ds;
    }
    public static List<QuyDinh> LayDanhSachQuyDinh()throws Exception{
        List<QuyDinh> ds = null;
        Session session = NewHibernateUtil.getSessionFactory().openSession();
        String hql = "Select qd From QuyDinh qd";
        Query query = session.createQuery(hql);
        ds = query.list();
        session.close();
        return ds;
    }
    
    public static boolean CapNhatQuyDinh(QuyDinh quyDinh)  {
        Session session=NewHibernateUtil.getSessionFactory().openSession();
       // String id = String.valueOf(quyDinh.getId());
        String id = String.valueOf(quyDinh.getId());
        if(QuyDinhDAO.LayDanhSachQuyDinhByID(id)== null){
            return false;
        }
        Transaction transaction=null;
        try{
            transaction = session.beginTransaction();
            session.update(quyDinh);
            transaction.commit();
        }
        catch(HibernateException exception){
            transaction.rollback();
            session.close();
           // throw new Exception(exception.getMessage());
        }
        finally{
         session.close(); 
        } 
        return true;
    }
    
    public static int ThemQuyDinh(QuyDinh quyDinh) throws Exception{
        int id=0;
        Session session=NewHibernateUtil.getSessionFactory().openSession();
        Transaction transaction=session.beginTransaction();
        try{
            id=(int) session.save(quyDinh);
            transaction.commit();
            session.close();
        }
        catch(HibernateException exception){
            transaction.rollback();
            session.close();
            throw new Exception(exception.getMessage());
        }
        return id;
    }
}
    /*public static boolean XoaQuyDinh(int ID) throws Exception{
        Session session=NewHibernateUtil.getSessionFactory().openSession();
        QuyDinh quyDinh = QuyDinhDAO.LayQuyDinhTheoID(ID);
        if(quyDinh == null){
            return false;
        }
        Transaction transaction=session.beginTransaction();
        try{
            session.delete(quyDinh);
            transaction.commit();
        }
        catch(HibernateException exception){
            transaction.rollback();
            session.close();
            throw new Exception(exception.getMessage());
        }
        finally{
         session.close(); 
        } 
        return true;*/
  //  }
    
   
//}

